package com.lanelu.hencoderplusstudy.view.practice9;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;


/**
 * 需要把它写成正方形的 ImageView
 */
public class Practice01SquareImageView extends AppCompatImageView {

    public Practice01SquareImageView(Context context) {
        super(context);
    }

    public Practice01SquareImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Practice01SquareImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 重写 onMeasure()，调整尺寸，让 ImageView 总是正方形
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//
//        // 先用 getMeasuredWidth() 和 getMeasuredHeight() 取到 super.onMeasure() 的计算结果
//        int measuredWidth = getMeasuredWidth();
//        int measuredHeight = getMeasuredHeight();
//        // 然后通过计算，让宽度和高度一致
//        if (measuredWidth > measuredHeight) {
//            measuredWidth = measuredHeight;
//        } else {
//            measuredHeight = measuredWidth;
//        }
//
//        // 再用 setMeasuredDimension(width, height) 来保存最终的宽度和高度
//        setMeasuredDimension(measuredWidth, measuredHeight);

        int measuredWidth = 200;
        int measuredHeight = 200;
        measuredWidth = resolveSize(measuredWidth, widthMeasureSpec);
        measuredHeight = resolveSize(measuredHeight, heightMeasureSpec);

        setMeasuredDimension(measuredWidth, measuredHeight);

    }

}
